#include"fragmentWidget.h"

FragmentWidget::FragmentWidget(QWidget* p):QWidget(p),ui(new Ui::FragmentWidget),filePath("")
{
    ui->setupUi(this);
    setInputMask();
}

FragmentWidget::~FragmentWidget()
{
    delete ui;
}

void FragmentWidget::setInputMask()
{

}

Fragment FragmentWidget::getFragmentFromUI()
{
    Fragment f;
    f.id = (qint32) ui->lineEdit_fragment_id->text().toInt();
    f.name = ui->lineEdit_fragment_name->text();
    f.description = ui->plainTextEdit_fragment_description->toPlainText();
    return f;
}

void FragmentWidget::displayFragmentToUI(Fragment& f)
{
    clearUI();

    ui->lineEdit_fragment_id->setText(QString::number(f.id));
    ui->lineEdit_fragment_name->setText(f.name);
    ui->plainTextEdit_fragment_description->setPlainText(f.description);
}

Fragment FragmentWidget::readFragmentFromFile()
{
    Fragment frag;

    QFile f(filePath);
    if(f.exists())
    {
        f.open(QIODevice::ReadOnly);
        QDataStream in(&f);
        in>>frag;
        f.close();
        displayFragmentToUI(frag);
    }else
        WARNING_FILE_NOT_EXIST;

    return frag;
}

bool FragmentWidget::saveFragmentToFile()
{
    if(ui->lineEdit_fragment_name->text().isEmpty()||filePath.isEmpty())
    {
        WARNING_NO_NAME;
        return false;
    }
    else
    {
        Fragment frag = getFragmentFromUI();
        QFile f(filePath);
        f.open(QIODevice::WriteOnly);
        QDataStream out(&f);
        out<<frag;
        f.close();

        int idx1 = filePath.lastIndexOf("/");
        int idx2 = filePath.lastIndexOf(".");

        filePath.replace(idx1+1,idx2-idx1-1,ui->lineEdit_fragment_name->text());
        f.rename(filePath);
        return true;
    }
}

void FragmentWidget::clearUI()
{
    ui->lineEdit_fragment_id->clear();
    ui->lineEdit_fragment_name->clear();
    ui->plainTextEdit_fragment_description->clear();
}

void FragmentWidget::exportToBinary(QString workspacepath)
{
    //export fragment
    QFile f5(workspacepath+"/"+FRAGMENT_EXPORT_NAME);
    f5.open(QIODevice::WriteOnly);
    QDataStream out5(&f5);

    out5<<Fragment::magic;
    out5<<Fragment::version;

    QDir dir_fragment(workspacepath+"/"+FRAGMENT_STR);
    QString mfilter = QString("*")+QString(FRAGMENT_FILE_SUFFIX);
    QStringList fragmentList = dir_fragment.entryList(QStringList(mfilter));
    for(int i=0;i<fragmentList.length();i++)
    {
        Fragment s;
        QFile f51(dir_fragment.absolutePath()+"/"+fragmentList.at(i));
        f51.open(QIODevice::ReadOnly);
        QDataStream in(&f51);
        in>>s;
        out5<<s;
        f51.close();
    }

    f5.close();

}

void FragmentWidget::exportToXML(QString workspacepath)
{
    //*****write XML*******//
    QFile file(workspacepath+"/output/fragment.xml");
    file.open(QIODevice::WriteOnly);
    QDomDocument doc;
    QDomText text;
    QDomProcessingInstruction instruction;
    instruction=doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"gb18030\"");
    doc.appendChild(instruction);
    QDomElement root=doc.createElement("root");
    doc.appendChild(root);

    QDir dir_fragment(workspacepath+"/"+FRAGMENT_STR);
    QStringList fragmentList = dir_fragment.entryList(QStringList()<<(QString("*")+FRAGMENT_FILE_SUFFIX));
    int cnt_fragment = fragmentList.length();
    for(int i=0;i<cnt_fragment;i++)
    {

        QDomElement fragment=doc.createElement("fragment");
        root.appendChild(fragment);

        Fragment e;
        QFile mf(dir_fragment.absolutePath()+"/"+fragmentList.at(i));
        mf.open(QIODevice::ReadOnly);
        QDataStream in(&mf);
        in>>e;
        mf.close();

        QDomElement element;

        element=doc.createElement("id");
        text=doc.createTextNode(QString::number(e.id));
        element.appendChild(text);
        fragment.appendChild(element);

        element=doc.createElement("name");
        text=doc.createTextNode(e.name);
        element.appendChild(text);
        fragment.appendChild(element);

        element=doc.createElement("description");
        text=doc.createTextNode(e.description);
        element.appendChild(text);
        fragment.appendChild(element);
    }

    QTextStream out(&file);
    doc.save(out,4);
    file.close();
}

void FragmentWidget::importFromXML(QString workspacepath,QString xmlFile)
{

}
